package com.yupi.yuapiinterface.controller;

import com.yupi.yuapiclinksdk.model.User;
import com.yupi.yuapiclinksdk.utils.SignUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 名称 API
 *
 * @author yupi
 */
@RestController
@RequestMapping("/")
public class NameController {
    @GetMapping("/name/get")
    public String getNameByGet(String name, HttpServletRequest request) {
        System.out.println(request.getHeader("yupi"));
        return "GET 你的名字是" + name;
    }

    @PostMapping("/name/post")
    public String getNameByPost(@RequestParam String name) {
        return "POST 你的名字是" + name;
    }

    @PostMapping("/name/requestbody")
    public String getUserNameByPost(@RequestBody User user, HttpServletRequest request) {
//        // 1.获取参数
//        String accessKey = request.getHeader("accessKey");
//        // 从请求头中获取名为 "secretKey" 的值
////        String secretKey = request.getHeader("secretKey");
//        String timestamp = request.getHeader("timestamp");
//        String nonce = request.getHeader("nonce");
//        String sign = request.getHeader("sign");
//        String body = request.getHeader("body");
//
//        // todo 实际情况应该是去数据库中查是否已分配给用户
//        if (!accessKey.equals("lihanqi")){
//            throw new RuntimeException("无权限");
//        }
//        // 校验随机数，模拟一下，直接判断nonce是否大于10000
//        if (Long.parseLong(nonce) > 10000) {
//            throw new RuntimeException("无权限");
//        }
//
//        // todo 时间和当前时间不能超过5分钟
////        if (timestamp) {}
//        String genSign = SignUtils.genSign(body, "12345678");
//        if(!genSign.equals(sign)){
//            throw new RuntimeException("无权限");
//        }
        String result = "POST 用户名字是" + user.getUsername();
        return result;
    }

    @GetMapping("/data")
    public String getData(HttpServletRequest request) {
        //获取当天日期并返回 比如2025-2-9
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return "今天的日期是"+currentDate.format(formatter);
    }

}
